const baseUrl = 'http://192.168.10.103:3000'; 
const store = {

    namespaced: true,
    state: {
        baseUrl,
        token: null,
        initStatus:false
    },
    
    getters: {},
    
    mutations: {
        setToken(state, token){
            state.token = token;
        },
        setinitStatus(state, data){
            state.initStatus = data;
        }
    },
 
    actions:{

        upFile({state}, filePath){
            
            return promisify(wx.uploadFile, {
                url: `${state.baseUrl}/upload`,
                filePath: filePath,
                name: 'file',
                head: { "Content-Type": "multipart/form-data" },
            })
        },

        upTag({dispatch}, data){
            return dispatch('request', {
                url: 'lists',
                data:data,
                method: 'POST'
            })
        },

        getWXUserInfo({state, commit}, data){

            let end =  promisify(wx.getUserInfo, {data})

            return end.then(resData =>{
                commit('setUserInfo', resData.userInfo, {root:true})
                return true;
            }).catch(err =>{
                console.log(err)
                return false
            })
        },

        async getRecommends({dispatch}){

            let {data} = await dispatch('request',{
                url: 'recommend',
            })
            return data.res;
        },
        
        async deleteTag({state, commit, dispatch}, id){

            let resData = await dispatch('request', {
                url:`lists/id=${id}`,
                method: 'DELETE'
            });
            return resData.data.statusCode === 200;
        },
       
        async login({state, commit, dispatch}){

            let {code} = await promisify(wx.login)
            let resData = await dispatch('request', {
                url: 'login',
                data: {loginCode: code},
                method: 'POST'
            })

            let end = resData.statusCode === 200;

            if(end){
                commit('setToken', resData.data.token)
            }
            
            return end;
           
        },

        async localUserInfo({state, dispatch, commit}){

            let {data} = await dispatch('request',{
                url: 'users',
                method: 'GET'
            });
            commit('setLocalUserInfo', data, {root:true})
        },

        /**
         * @name 获取标签列表
         * @param {string} id tagId 标签编号
         */
        async getLists({state, commit, dispatch}, id){

            let {data} = await dispatch('request',{
                url: 'lists',
                data: id ? {id} : {},
            })
            return data.res;
        },
        
        async request({state}, data){
            
            wx.showLoading({
                title: '加载中'
            })

            let {token, baseUrl} = state;
            let reqConfig = Object.assign({}, data, {
                url: `${baseUrl}/${data.url}`,
                data: data.data,
                header: {Authorization: `Bearer ${token}`},
            })
            
            return await promisify(wx.request, reqConfig).then((data) =>{
                wx.hideLoading();
                return data
            });
        }
    }
    
}



/**
 * @name 包装wx请求，使用promise形式
 * @param {function} fn wx请求函数 
 * @param {any} data 函数参数 
 */
function promisify(fn, data={}){

    return new Promise((resolve, reject) =>{

        data = Object.assign(data, {
            success: resolve,
            fail: reject
        })

        fn(data)
    })
}



export default store